<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Nameplate extends Base
{
    protected $table = 'nameplate';
    protected $nameplateUserTable = 'nameplate_user';

    //铭牌缓存
    protected $nameplateKey = 'NAMEPLATE:DATA';//string NAMEPLATE:DATA => {string}
    //铭牌用户缓存
    protected $nameplateUserKey = 'NAMEPLATE:USERDATA:';//string NAMEPLATE:USERDATA:{userId} => {string}

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //新增铭牌
    public function addNameplate($nameplateName, $nameplateImg, $nameplateResource, $nameplateDesc)
    {
        $data = [];
        $data['nameplate_name'] = $nameplateName;
        $data['nameplate_img'] = getUrlPath($nameplateImg);
        $data['nameplate_resource'] = getUrlPath($nameplateResource);
        $data['nameplate_desc'] = $nameplateDesc;

        $id = DB::table($this->table)->insertGetId($data);

        $cacheKey = $this->nameplateKey;
        redis::DEL($cacheKey);

        return $id;
    }

    //编辑铭牌
    public function editNameplate($nameplateId, $params)
    {
        $upData = [];
        if(!empty($params['nameplate_name'])) {
            $upData['nameplate_name'] = $params['nameplate_name'];
        }
        if(!empty($params['nameplate_img'])) {
            $upData['nameplate_img'] = getUrlPath($params['nameplate_img']);
        }
        if(!empty($params['nameplate_resource'])) {
            $upData['nameplate_resource'] = getUrlPath($params['nameplate_resource']);
        }
        if(isset($params['nameplate_desc'])) {
            $upData['nameplate_desc'] = $params['nameplate_desc'];
        }
        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->table)->where('id', $nameplateId)->where('is_delete', 0)->update($upData);

        $cacheKey = $this->nameplateKey;
        redis::DEL($cacheKey);

        return $res;
    }

    /**
     * 根据id获取铭牌
     */
    public function getNameplateById($nameplateId){
        $data = DB::table($this->table)
            ->where('is_delete', 0)
            ->where('id', $nameplateId)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 获取铭牌数据
     */
    public function getNameplateAll(){
        $cacheKey = $this->nameplateKey;
        $list = Redis::get($cacheKey);
        if(empty($list)){
            $list = DB::table($this->table)
                ->where('is_delete', 0)
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($cacheKey, 86400, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }
        foreach($list as $key=>$val) {
            $list[$key]['nameplate_img'] = build_url($val['nameplate_img']);
            $list[$key]['nameplate_resource'] = build_url($val['nameplate_resource']);
            $list[$key] = $val;
        }
        $list = array_column($list, null, "id");

        return $list;
    }

    /**
     * 获取指定铭牌指定用户数据
     */
    public function getNameplateUser($nameplateId, $userId){
        $data = DB::table($this->nameplateUserTable)
            ->where('is_delete', 0)
            ->where('nameplate_id', $nameplateId)
            ->where('user_id', $userId)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }

    //新增铭牌用户
    public function addNameplateUser($nameplateId, $userId, $timeSt, $timeEt, $sort, $adminId)
    {
        $data = [];
        $data['nameplate_id'] = (int) $nameplateId;
        $data['user_id'] = (int) $userId;
        $data['time_st'] = $timeSt;
        $data['time_et'] = $timeEt;
        $data['sort'] = (int) $sort;
        $data['admin_id'] = (int) $adminId;

        $id = DB::table($this->nameplateUserTable)->insertGetId($data);

        $cacheKey = $this->nameplateUserKey.$userId;
        redis::DEL($cacheKey);

        return $id;
    }

    //编辑铭牌
    public function editNameplateUser($nameplateId, $userId, $params)
    {
        $upData = [];
        if(!empty($params['time_st'])) {
            $upData['time_st'] = $params['time_st'];
        }
        if(!empty($params['time_et'])) {
            $upData['time_et'] = $params['time_et'];
        }
        if(!empty($params['admin_id'])) {
            $upData['admin_id'] = $params['admin_id'];
        }
        if(isset($params['sort'])) {
            $upData['sort'] = (int) $params['sort'];
        }
        if(isset($params['is_delete'])) {
            $upData['is_delete'] = (int) $params['is_delete'];
        }
        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->nameplateUserTable)->where('nameplate_id', $nameplateId)->where('user_id', $userId)->where('is_delete', 0)->update($upData);

        $cacheKey = $this->nameplateUserKey.$userId;
        redis::DEL($cacheKey);

        return $res;
    }

    /**
     * 根据用户ID获取铭牌信息
     */
    public function getNameplateByUserId($userId){
        $now = time();
        $cacheKey = $this->nameplateUserKey.$userId;
        $list = Redis::get($cacheKey);
        if(empty($list)){
            $list = DB::table($this->nameplateUserTable)
                ->where('is_delete', 0)
                ->where('user_id', $userId)
                ->select(['id','nameplate_id','user_id','sort','time_st','time_et'])
                ->orderBy('sort', 'asc')
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($cacheKey, 86400, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }

        //铭牌
        $nameplateList = $this->getNameplateAll();

        $data = [];
        foreach($list as $val) {
            if(empty($nameplateList[$val['nameplate_id']])) {
                continue;
            }
            if(strtotime($val['time_st'])<=$now && $now<=strtotime($val['time_et'])) {
                $val['nameplate_name'] = build_url($nameplateList[$val['nameplate_id']]['nameplate_name']);
                $val['nameplate_desc'] = build_url($nameplateList[$val['nameplate_id']]['nameplate_desc']);
                $val['nameplate_img'] = build_url($nameplateList[$val['nameplate_id']]['nameplate_img']);
                $val['nameplate_resource'] = build_url($nameplateList[$val['nameplate_id']]['nameplate_resource']);

                $data[] = $val;
            }
        }

        return $data;
    }

}
